import React, { Component } from 'react'
import { connect } from 'react-redux'
import {Typography, Row} from 'antd'

const { Paragraph } = Typography

class OrganizationLink extends Component {
  hashCode (str) {
    let h = 0
    let off = 0
    let len = str.length
    for (let i = 0; i < len; i++) {
      h = 31 * h + str.charCodeAt(off++)
    }
    return h + ''
  }

  encrypt (organizationId) {
    const enable = 1
    return this.hashCode(`${organizationId}${enable}`) + '.' + this.hashCode(`""${organizationId}${organizationId}`) + '.' + organizationId
  }

  getRegisterUrl () {
    let {settings, user} = this.props
    const code = this.encrypt(user.currentOrganizationId)
    const context = settings.appContextPath === '/learn' ? '' : 'staging.'
    const link = `http://${context}school.thoughtworks.cn/learn/auth/register?org=${code}`
    return link
  }

  render () {
    return (
      <Row type='flex' align='center' justify='center'>
        <Paragraph copyable>{this.getRegisterUrl()}</Paragraph>
      </Row>
    )
  }
}

const mapStateToProps = ({settings, user}) => ({
  user, settings
})

export default connect(mapStateToProps, null)(OrganizationLink)
